package com.zjitc.pigstudy.controller;

import com.zjitc.pigstudy.Exception.ServiceException;
import com.zjitc.pigstudy.common.AuthAccess;
import com.zjitc.pigstudy.common.Result;
import com.zjitc.pigstudy.domain.Time;
import com.zjitc.pigstudy.domain.User;
import com.zjitc.pigstudy.domain.UserDTO;
import com.zjitc.pigstudy.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@CrossOrigin
@RestController
@Slf4j
public class WebController {

    @Autowired
    private UserService userService;

    //登录
    @PostMapping("/login")
    @ResponseBody
    public Result login(@RequestBody User user){
        if("".equals(user.getPassword()) || "".equals(user.getAccount()==null)){
            return Result.error("输入不合法");
        }

        user = userService.login(user);

        log.info("查询成功，数据为：{}",user);
        return Result.success(user);
    }
    //注册
    @PostMapping("/register")
    @ResponseBody
    public Result register(@RequestBody User user){

            user = userService.register(user);

        log.info("插入成功，数据为：{}",user);
        return Result.success(user);
    }

    @PostMapping("/selectAll")
    @ResponseBody
    public Result selectAll(){

        List<User> user = userService.selectAll();
        return Result.success(user);
    }

    @PutMapping("/updateUser")
    @ResponseBody
    public Result updateUser(@RequestBody User user){
        System.out.println(user);
        userService.updateUser(user);
        log.info("修改成功");
        return Result.success(null);
    }

    @AuthAccess
    @PutMapping("/resetPassword")
    @ResponseBody
    public Result resetPassword(@RequestBody User user){
        userService.resetPassword(user);
        log.info("修改密码成功");
        return Result.success("");
    }

    @PostMapping("/getLoginTime")
    @ResponseBody
    public Result getLoginTime(@RequestBody User user){
        Time time = userService.getLoginTime(user.getId());
        log.info("获取时间成功{}",time.getLoginTime());
        return Result.success(time);
    }

    @PostMapping("/setLoginTime")
    @ResponseBody
    public Result setLoginTime(@RequestBody Time time){
        System.out.println(time);
        userService.setLoginTime(time);
        log.info("设置成功：{}",time.getLoginTime());
        return Result.success("");
    }
}
